package com.kaishengit.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.SaleDao;
import com.kaishengit.entity.All;
import com.kaishengit.entity.Page;
import com.kaishengit.service.SaleServic;
import com.kaishengit.util.Zhuanma;

public class SearchServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String task = request.getParameter("task");
		if ("search".equals(task)) {
			search(request, response);
		}
	}

	private void search(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String danwei = Zhuanma.IsoToUtf(request.getParameter("danwei"));
		String tr = Zhuanma.IsoToUtf(request.getParameter("tr"));
		String minnum = Zhuanma.IsoToUtf(request.getParameter("minnum"));
		String maxnum = Zhuanma.IsoToUtf(request.getParameter("maxnum"));
		String from = Zhuanma.IsoToUtf(request.getParameter("from"));
		String mintotal = Zhuanma.IsoToUtf(request.getParameter("mintotal"));
		String maxtotal = Zhuanma.IsoToUtf(request.getParameter("maxtotal"));
		SaleServic ser = new SaleServic();
		String where = ser.getWhere(starttime, endtime, danwei, tr, minnum,maxnum, from,
				mintotal, maxtotal);
		
		SaleDao saleDao = new SaleDao();

		List<All> list = saleDao.findSerach(where);
		
		SaleDao saledao = new SaleDao();
		HttpSession session = request.getSession();
		int p = 0;
		Object o = session.getAttribute("page");
		
		if (o != null) {
			p = (Integer) o;
		} else {
			p = 0;
		}

	
		Page page = new Page(p, 5, saledao.getSearchCount(where));
		page = saledao.findAllsBySearch(page, where);

		List<String> listStr = new ArrayList<String>();
		if (page.getMaxPage() < 7) {
			for (int i = 0; i < page.getMaxPage(); i++) {
				listStr.add(String.valueOf(i + 1));
			}
		} else {
			if ((page.getNowPage()) < 4) {
				for (int i = 1; i < 7; i++) {
					listStr.add(String.valueOf(i));
				}
			} else {
				if (page.getNowPage() > (page.getMaxPage() - 4)) {
					for (int i = (page.getMaxPage() - 6); i < page.getMaxPage(); i++) {
						listStr.add(String.valueOf(i + 1));
					}
				} else {
					listStr.add(String.valueOf(page.getNowPage() - 1));
					listStr.add(String.valueOf(page.getNowPage()));
					listStr.add(String.valueOf(page.getNowPage() + 1));
					listStr.add(String.valueOf(page.getNowPage() + 2));
					listStr.add(String.valueOf(page.getNowPage() + 3));
					listStr.add(String.valueOf(page.getNowPage() + 4));
				}
			}
		}
		int sumpeople = 0;
		float sumtotal = 0f;
		for (All a : page.getList()) {
			sumtotal = sumtotal + a.getTotalmoney();
			sumpeople = sumpeople + a.getNum();
		}
		request.setAttribute("all", list);
		request.setAttribute("pagenum", listStr);
		request.setAttribute("page", page);
		request.setAttribute("nowpage", page.getNowPage());
		request.setAttribute("all", page.getList());
		request.setAttribute("sum", page.getList().size());
		request.setAttribute("sumtotal", sumtotal);
		request.setAttribute("sumpeople", sumpeople);
		request.setAttribute("unitpeople", sumtotal / sumpeople);

		request.getRequestDispatcher("WEB-INF/view/main.jsp").forward(request,
				response);

	}

}
